package org.example.springmybatisdemo.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.springmybatisdemo.entity.CarOrder;
import org.example.springmybatisdemo.entity.CarUser;
import org.example.springmybatisdemo.entity.Dept;
import org.example.springmybatisdemo.entity.Result;
import org.example.springmybatisdemo.service.CarServiceImpl;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "购车订单")
@RequestMapping("/cars") // 基础路径
@RestController
public class CarController {
    @Resource(name = "carServiceImpl") // 要小写
    private CarServiceImpl carService;

    @Operation(summary = "购车用户模块登录")
    @PostMapping("/login")
    public Result<List<CarUser>> login(@RequestBody CarUser user) {
        try {
            //        1. 调用service获取数据
            List<CarUser>  carlist = carService.login(user);

            //        2. 响应数据
            return Result.success(carlist) ;
        } catch (RuntimeException e) {
            return Result.fail(e.getMessage(), 400);
        }
    }

//    订单列表
@Operation(summary = "购车订单")
    @GetMapping("/api/orderlist")
    public  Result<List<CarOrder>> list(@RequestParam Integer userId) {
        //        1. 调用service获取数据
        List<CarOrder> carorderlist = carService.orderlist(userId);

        //        2. 响应数据
        return Result.success(carorderlist) ;
    }

    //新增订单
    @Operation(summary = "新增购车订单")
    @PostMapping("/addorder") // @RequestBody用来接受json格式数据
    public Result<Void> add(@RequestBody CarOrder carorder) {
        carService.addOrder(carorder);
        return Result.success();
    }

}

